package com.ymatou.shop.ui.view;

import com.ymatou.shop.R;

import android.widget.ViewSwitcher;

import android.R.integer;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ToggleButtonLand extends ViewSwitcher {
	private boolean toggleState = false;
	private Animation inFromRight;
	private Animation outToLeft;
	private Animation inFromLeft;
	private Animation outToRight;
	private OnToggleChangedListener mListener;

	public ToggleButtonLand(Context context) {
		super(context);
	}

	public ToggleButtonLand(Context context, AttributeSet attrs) {
		super(context, attrs);
		inFromRight = inFromRightAnimation();
		inFromLeft = inFromLeftAnimation();
		outToLeft = outToLeftAnimation();
		outToRight = outToRightAnimation();

		LinearLayout.LayoutParams iLp = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		ImageView leftToggler = new ImageView(getContext());
		leftToggler.setLayoutParams(iLp);
		leftToggler.setBackgroundResource(R.drawable.toggle_thumb_off);

		LinearLayout rl = new LinearLayout(getContext());
		rl.setLayoutParams(iLp);
		rl.setGravity(Gravity.LEFT);
		rl.setBackgroundResource(R.drawable.toggle_btn_off_bg);
		rl.addView(leftToggler);
		addView(rl);

		ImageView rightToggler = new ImageView(getContext());
		rightToggler.setLayoutParams(iLp);
		rightToggler.setBackgroundResource(R.drawable.toggle_thumb_on);

		LinearLayout r2 = new LinearLayout(getContext());
		r2.setLayoutParams(iLp);
		r2.setGravity(Gravity.RIGHT);
		r2.setBackgroundResource(R.drawable.toggle_btn_on_bg);
		r2.addView(rightToggler);
		addView(r2);

		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (toggleState) {
					ToggleButtonLand.this.setInAnimation(inFromRight);
					ToggleButtonLand.this.setOutAnimation(outToLeft);
					ToggleButtonLand.this.setDisplayedChild(0);
				} else {
					ToggleButtonLand.this.setInAnimation(inFromLeft);
					ToggleButtonLand.this.setOutAnimation(outToRight);
					ToggleButtonLand.this.setDisplayedChild(1);
				}
				toggleState = !toggleState;
				if (mListener != null)
					mListener.toggleChanged(v, toggleState);
			}
		});
	}
	
    private Animation inFromRightAnimation() {
        Animation inFromRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromRight.setDuration(250);
        return inFromRight;
    }
    
    private Animation inFromLeftAnimation() {
        Animation inFromRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        inFromRight.setDuration(250);
        return inFromRight;
        
    }
    
    private Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }
    
    private Animation outToRightAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
        );
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }
    
	public boolean getToggleState() {
		return toggleState;
	}

	public static interface OnToggleChangedListener {
		void toggleChanged(View v, boolean value);
	}

	public void setOnToggleChangedListener(OnToggleChangedListener listener) {
		mListener = listener;
	}

	public void setOn() {
		ToggleButtonLand.this.setDisplayedChild(1);
		toggleState = true;
	}

	public void setOff() {
		ToggleButtonLand.this.setDisplayedChild(0);
		toggleState = false;
	}
}
